package com.rwy.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.rwy.dao.HandleFaultDaoMapper;
import com.rwy.dto.device.EmployeeDTO;
import com.rwy.dto.device.FaultCategoryDTO;
import com.rwy.dto.device.HandleFaultDeviceItemDTO;
import com.rwy.dto.device.HandleFaultRecordDTO;
import com.rwy.dto.device.HandleFaultTaskDTO;
import com.rwy.util.PageInfo;
import com.rwy.util.constant.FaultDeviceEnum;

@Service
public class HandleFaultDeviceService {
 
	@Autowired
	HandleFaultDaoMapper faultDao;
	
	
	public List<FaultCategoryDTO> getFaultCategory() {
		List<FaultCategoryDTO> source = faultDao.getFaultCategoryList();
		List<FaultCategoryDTO> list = source.stream().filter(s -> s.getParentID() == 0).collect(Collectors.toList());
		
		Map<Integer, List<FaultCategoryDTO>> map = source.stream().filter(s -> s.getCategoryType() == 2).collect(Collectors.groupingBy(FaultCategoryDTO::getParentID));
		return list.stream().map(s -> { 
			 return FaultCategoryDTO.builder().categoryId(s.getCategoryId())
				 .categoryName(s.getCategoryName()).childList(map.get(s.getCategoryId())).build();}).collect(Collectors.toList());
	} 
	
	@Transactional
	public void createFaultTask() {
		faultDao.createFaultTask();
		faultDao.createDisconnectTeriminal();
		faultDao.updateDisconnectTeriminal();
		faultDao.createAdviceTask();
	}
	
	@Transactional
	public void updateFaultHandling(HandleFaultTaskDTO task) {
		Assert.notNull(task);
		faultDao.updateFaultHandling(task);
	}
	
	@Transactional
	public void updateHandleAdvice(HandleFaultTaskDTO task) {
		faultDao.updateHandlAdvice(task);
	}
	
	public List<HandleFaultTaskDTO> getWaitAdviceTask() {
		
		 return faultDao.adviceFaultTaskForEmployee();
	}
	
	public EmployeeDTO getEmployMesByMobile(String mobile) {
	
		return faultDao.getEmployeeByMobile(mobile);
	}
	
	public HandleFaultDeviceItemDTO getDeivceMessage(Integer terminalId,Integer handID) {
		return faultDao.getDeviceMessage(terminalId, handID);
	}
	
	@SuppressWarnings("unchecked")
	public PageInfo<HandleFaultDeviceItemDTO> gethandleFaultItem(HandleFaultDeviceItemDTO param) {
		PageInfo<HandleFaultDeviceItemDTO> data = PageInfo.getInstance();
		int count = faultDao.getFaultDeviceTaskItemCount(param.getHandStatus(), param.getEmployeeId());
		List<HandleFaultDeviceItemDTO> list = faultDao.getFaultDeviceTaskItemByParam(param.getHandStatus(), param.getEmployeeId(), param.getPage(), param.getPageSize());
		return data.assemPaInfo(param.getPage(),count, param.getPageSize(), list);
	}

	public HandleFaultTaskDTO getFaultDeviceTaskByHandIDAndEmployeeID(int employeeID, int handID) {
		return faultDao.getFaultDeviceTask(employeeID, handID);
	}

	@Transactional
	public void saveMaintainRecord(HandleFaultRecordDTO record) {
		 Assert.notNull(record);
		 Assert.notNull(record.getHandID());
		 Assert.notNull(record.getRecordStatus());
		// Assert.notNull(faultDao.getFaultDeviceTask(record.getEmployID(), record.getHandID()));
		 faultDao.insertFaultHandlingRecord(record);
		//更新task
		 updateFaultHandling(HandleFaultTaskDTO.builder().handID(record.getHandID())
				 .handStatus(record.getRecordStatus()==FaultDeviceEnum.record_help_task.getId()? FaultDeviceEnum.task_help_task.getId()
						  :(record.getRecordStatus() == FaultDeviceEnum.record_processed_task.getId() ? FaultDeviceEnum.task_processed_task.getId()
					      : FaultDeviceEnum.task_wait_handle.getId()))
				 .updateUser(record.getEmployID())
				 .build());
	}
	
    public HandleFaultRecordDTO gethandleRecordDetail(Integer type, Integer employeeId, Integer handID) {
    	return faultDao.getHandleReocrdDetail(type, handID);
    }
    
    public void syncNormalTerminalStatus() {
    	faultDao.syncNormalTerminalStatus();
    }
}
